package com.sample.logic;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.sample.constants.ServerConst;
import com.sample.info.URLInfo;
import com.sample.util.Util;

/**
 * GET通信用ロジック
 * @author SASAKI
 *
 */
public class HttpStandardLogic {

	/** URL(LocalTomcat) */
	private URLInfo urlInfo = new URLInfo(ServerConst.PROTCOL_HTTP, // http
											ServerConst.HOST_LOCAL, // 10.0.2.2
											ServerConst.PORT_8080, // Tomcat ListenPort
											ServerConst.APP_NAME, // Tomcat App Name
											ServerConst.STANDARD_SERVLET
											);
	/** 実機向け */
//	private URLInfo urlInfo = new URLInfo(ServerConst.PROTCOL_HTTP,
//			ServerConst.HOST_GAE,
//			null,
//			null,
//			ServerConst.STANDARD_SERVLET
//			);

	/**
	 * HTTP GET通信を行う.
	 * @param param1 パラメータ1
	 * @param param2 パラメータ2
	 * @return 通信結果
	 */
	public String sendHttpGet(String param1, String param2) {

		HttpClient hc = new DefaultHttpClient();

		// URLにパラメータを付与
		StringBuffer sb = new StringBuffer(500);
		sb.append(urlInfo.toUrlString());
		sb.append("?param1=" + param1);
		sb.append("&param2=" + param2);

		HttpGet httpGet = new HttpGet(sb.toString());
		try {
			// 送信
			HttpResponse res = hc.execute(httpGet);

			sb.append("\n");

			// 通信判定(正常:200,201)
			int status = res.getStatusLine().getStatusCode();
			if (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED) {
				HttpEntity entity = res.getEntity();
				String result = EntityUtils.toString(entity);
				sb.append(result);
			} else {
				sb.append("通信に失敗しました。\n");
				sb.append("StatusCode:" + status + "\n");
				sb.append("Reason    :" + res.getStatusLine().getReasonPhrase());
			}

		} catch (ClientProtocolException cpex) {
			sb.append(Util.getStackTraceString(cpex));
		} catch (IOException ioex) {
			sb.append(Util.getStackTraceString(ioex));
		}

		return sb.toString();
	}

	/**
	 * HTTP POST通信を行う.
	 * @param param1 パラメータ1
	 * @param param2 パラメータ2
	 * @return 通信結果
	 */
	public String sendHttpPost(String param1, String param2) {

		HttpClient hc = new DefaultHttpClient();

		StringBuffer sb = new StringBuffer(500);
		sb.append(urlInfo.toUrlString());


		HttpPost httpPost = new HttpPost(sb.toString());

		// これはNG
//		HttpParams params = hc.getParams();
//		params.setParameter("param1", param1);
//		params.setParameter("param2", param2);
//		httpPost.setParams(params);

		List<NameValuePair> postParams = new ArrayList<NameValuePair>();
		postParams.add(new BasicNameValuePair("param1", param1));
		postParams.add(new BasicNameValuePair("param2", param2));

		try {
			/* POSTの場合はHttpリクエストボディに設定する必要があるため、
			   PostのEntity(ボディ)へ  */
			httpPost.setEntity(new UrlEncodedFormEntity(postParams));

			// 送信
			HttpResponse res = hc.execute(httpPost);

			sb.append("\n");

			// 通信判定(正常:200,201)
			int status = res.getStatusLine().getStatusCode();
			if (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED) {
				HttpEntity entity = res.getEntity();
				String result = EntityUtils.toString(entity);
				sb.append(result);
			} else {
				sb.append("通信に失敗しました。\n");
				sb.append("StatusCode:" + status + "\n");
				sb.append("Reason    :" + res.getStatusLine().getReasonPhrase());
			}

		} catch (ClientProtocolException cpex) {
			sb.append(Util.getStackTraceString(cpex));
		} catch (IOException ioex) {
			sb.append(Util.getStackTraceString(ioex));
		}

		return sb.toString();
	}
}
